//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <elastos.h>

_ELASTOS_NAMESPACE_USING
#include "CConsoleFile.h"

#if defined(_win32) || defined(_wince) || defined(_linux)
ELAPI _CSystem_DebugErrorPrint(WString string);

CConsoleFile::CConsoleFile(int fd)
{
    m_ifd = fd;
}
#endif

CConsoleFile::CConsoleFile()
{
}

CConsoleFile::~CConsoleFile()
{
}

PInterface CConsoleFile::Probe(REIID riid)
{
    if (riid == EIID_IBasicFile) {
        return (IBasicFile *)this;
    }
    else if (riid == EIID_IAbsoluteIO) {
        return (IAbsoluteIO *)this;
    }
    else if (riid == EIID_IInterface) {
        return (IInterface *)(IAbsoluteIO *)this;
    }
    else {
        return NULL;
    }
}

UInt32 CConsoleFile::AddRef()
{
    return (UInt32)this->m_RefCnt.Increment();
}

UInt32 CConsoleFile::Release()
{
    UInt32 Refs;

    Refs = (UInt32)this->m_RefCnt.Decrement();
    if (Refs == 0) delete this;
    return Refs;
}

ECode CConsoleFile::Aggregate(AggregateType type, IInterface *pObj)
{
    return E_NOT_IMPLEMENTED;
}

ECode CConsoleFile::GetDomain(
    /* [out] */ IInterface **ppObj)
{
    return E_NOT_IMPLEMENTED;
}

ECode CConsoleFile::Read(
    /* [in] */ Int32 offset,
    /* [in] */ Int32 toRead,
    /* [out] */ MemoryBuf * pezbBuf)
{
    if (toRead == 0 || pezbBuf == NULL) {
        return E_INVALID_ARGUMENT;
    }

    return _Impl_GetChar(toRead, pezbBuf, TRUE);
}

ECode CConsoleFile::Write(
    /* [in] */ Int32 offset,
    /* [in] */ const MemoryBuf & ezbBuf,
    /* [out] */ Int32 * pWrite)
{
    wchar_t wszbuf[_MAX_LINE + 1];

    UInt32 len = ezbBuf.GetUsed();
    *pWrite = len;
    int cnt = 0;
    char *buf = (char *)ezbBuf.GetPayload();

    while (len > 0) {
        cnt = len > _MAX_LINE ? _MAX_LINE : len;
        for (int i = 0; i < cnt; i++) {
            if (*buf) {
                wszbuf[i] = (wchar_t)(*buf);
            }
            else {
                wszbuf[i] = L' ';
            }
            buf++;
        }
        wszbuf[cnt] = L'\0';
#if defined(_win32) || defined(_wince)
        if (m_ifd == 2)
            _CSystem_DebugErrorPrint(wszbuf);
        else
            _CSystem_DebugPrint(wszbuf);
#else
        _CSystem_DebugPrint(wszbuf);
#endif
        len -= cnt;
    }
    return NOERROR;
}

ECode CConsoleFile::Sync()
{
    return NOERROR;
}

ECode CConsoleFile::GetOptimalBlockSize(
    /* [out] */ Int32 * pSize)
{
    return NOERROR;
}

ECode CConsoleFile::GetType(
    /* [out] */ FileType *pType)
{
    return NOERROR;
}

ECode CConsoleFile::GetAccessTime(
    /* [out] */ Int32 * pTime)
{
    return NOERROR;
}

ECode CConsoleFile::SetAccessTime(
    /* [in] */ Int32 time)
{
    return NOERROR;
}

ECode CConsoleFile::GetModificationTime(
    /* [out] */ Int32 * pTime)
{
    return NOERROR;
}

ECode CConsoleFile::SetModificationTime(
    /* [in] */ Int32 time)
{
    return NOERROR;
}

ECode CConsoleFile::GetCreateTime(
    /* [out] */ Int32 * pTime)
{
    return NOERROR;
}

ECode CConsoleFile::CheckAccessMode(
    /* [in] */ FileAccessMode mode)
{
    return NOERROR;
}

ECode CConsoleFile::GetAccessMode(
    /* [out] */ FileAccessMode * pMode)
{
    return NOERROR;
}

ECode CConsoleFile::SetAccessMode(
    /* [in] */ FileAccessMode mode)
{
    return NOERROR;
}

ECode CConsoleFile::GetShareMode(
    /* [out] */ FileShareMode * pMode)
{
    return NOERROR;
}

ECode CConsoleFile::SetShareMode(
    /* [in] */ FileShareMode mode)
{
    return NOERROR;
}

ECode CConsoleFile::GetSize(
    /* [out] */ Int32 * pSize)
{
    return NOERROR;
}

ECode CConsoleFile::SetSize(
    /* [in] */ Int32 size)
{
    return NOERROR;
}
